/* eslint indent: ["off"] */
/* eslint space-before-function-paren: ["off"] */

// 热冷号算法

// 引入 lodash 库，用于数据处理和操作
const _ = require('lodash')
// 精确计算
const NP = require('number-precision')
// 引入 path 模块，用于处理文件和目录路径
const path = require('path')
// 获取项目根目录
const ROOT_DIR = path.join(__dirname, '..')
// helpers 目录
const HELPERS_DIR = path.join(ROOT_DIR, 'helpers')
// 读取所有数据
let { allValues } = require(path.join(HELPERS_DIR, 'readAllValues.js'))

// console.log(allValues.length)

// 使用 lodash 的 countBy 来统计每个 number 的次数
// const numberCounts = _.countBy(allValues, 'number')

// console.log(numberCounts)

// 按出现次数从高到低排序
// const sortedNumberCounts = _.orderBy(
//   Object.entries(numberCounts), // 将对象转换为数组
//   [([_, count]) => count], // 按次数排序
//   ['desc'] // 降序
// )

// 选取出现次数最多的前24个号码作为预测
// const top24Numbers = sortedNumberCounts.slice(0, 24).map(([number]) => number)
// console.log(top24Numbers)

// 使用 slice 来拆分
const first100 = allValues.slice(0, 32) // 获取前100个
const remaining = allValues.slice(32) // 获取剩余部分

const numberCounts = _.countBy(first100, 'number')

const getTop24Numbers = (numberCounts, take = 24) => {
  return (
    _.chain(numberCounts)
      // .countBy('number')
      .toPairs()
      .orderBy([1], ['desc'])
      .take(take)
      .map(([number]) => number)
      .value()
  )
}

// 前百个号码中出现次数最多的前24个号码
const sortedNumberCounts = getTop24Numbers(numberCounts, 32)
// console.log(sortedNumberCounts)

const result = remaining.map((item, index) => {
  let topNumbers
  if (index === 0) {
    topNumbers = sortedNumberCounts
  } else {
    if (numberCounts[item.number]) {
      numberCounts[item.number] += 1
    } else {
      numberCounts[item.number] = 1
    }
    topNumbers = getTop24Numbers(numberCounts, 32)
  }
  // console.log(topNumbers)
  return {
    ...item,
    accuracy: topNumbers.map(Number).includes(Number(item.number)) ? 1 : 0
  }
})

const accuracy = result.map((item) => item.accuracy)
// console.log(accuracy)

const accuracyCount = _.countBy(accuracy)
// console.log(accuracyCount)

const successRate = NP.times(NP.round(NP.divide(accuracyCount[1], accuracy.length), 2), 100)

console.log(`${accuracyCount[1]} / ${accuracy.length} = ${successRate}%`)

console.log('--------------------------------')

// 计算准确统计
const { calcAccuracySummary, ACCURACY_SUMMARY_CN_MAP } = require(path.join(
  HELPERS_DIR,
  'calcAccuracySummary.js'
))

const accuracySummary = calcAccuracySummary(accuracy)
console.log(accuracySummary)

// 打印中文
Object.entries(ACCURACY_SUMMARY_CN_MAP).forEach(([key, value]) => {
  console.log(`${value}: ${accuracySummary[key]}`)
})
  